package com.qxzn.app.personalcenter.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.LoginAndRegisterModel;
import com.qxzn.app.page.view.CustomItemView;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.ActivityStack;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.DateUtil;
import com.qxzn.app.util.DisplayUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册8-用户信息
 */
public class RegisterMsgActivity extends BaseActivity implements MyInterface.RegisterListener {

    @BindView(R2.id.civ_age)
    CustomItemView civAge;
    @BindView(R2.id.civ_height)
    CustomItemView civHeight;
    @BindView(R2.id.civ_weight)
    CustomItemView civWeight;
    @BindView(R2.id.civ_blood_pressure)
    CustomItemView civBloodPressure;
    private OptionsPickerView<String> heightPicker;
    private OptionsPickerView<String> weightPicker;
    private OptionsPickerView<String> pressurePicker;
    private List<String> heightList = new ArrayList<>();
    private List<String> weightList = new ArrayList<>();
    private List<String> heightPressureList = new ArrayList<>();
    private List<String> lowPressureList = new ArrayList<>();
    private TimePickerBuilder timeBuilder;
    private long timeStamp;
    private TimePickerView pvTime;
    private LoginAndRegisterModel loginAndRegisterModel;
    private String birth;
    private String height;
    private String weight;
    private String pressure;

    @Override
    protected int initLayout() {
        return R.layout.activity_register_msg;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loginAndRegisterModel = new LoginAndRegisterModel(this);
        loginAndRegisterModel.setRegisterListener(this);
    }

    @OnClick({R2.id.civ_age, R2.id.civ_height, R2.id.civ_weight, R2.id.civ_blood_pressure, R2.id.tv_next})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.civ_age) {
            showTimeSelect();
        } else if (i == R.id.civ_height) {
            showHeightSelect();
        } else if (i == R.id.civ_weight) {
            showWeightSelect();
        } else if (i == R.id.civ_blood_pressure) {
            showPressureSelect();
        } else if (i == R.id.tv_next) {
            if (CommonUtil.isFastClick()) {
                return;
            }
            if (TextUtils.isEmpty(birth)) {
                AppUtil.showToast(this, "请选择生日");
                return;
            }
            if (TextUtils.isEmpty(height)) {
                AppUtil.showToast(this, "请选择身高");
                return;
            }
            if (TextUtils.isEmpty(weight)) {
                AppUtil.showToast(this, "请选择体重");
                return;
            }
//            if (TextUtils.isEmpty(pressure)) {
//                AppUtil.showToast(this, "请选择血压");
//                return;
//            }
            showProgress();
            JSONObject params = new JSONObject();
            params.put("phone", getIntent().getStringExtra("phone"));
            params.put("pwd", getIntent().getStringExtra("pwd"));
            params.put("nickname", getIntent().getStringExtra("nickname"));
            params.put("security_pwd", getIntent().getStringExtra("security_pwd"));
            params.put("sex", getIntent().getIntExtra("sex", 1));
            params.put("birth", birth);
            params.put("height", height);
            params.put("weight", weight);
            loginAndRegisterModel.register(params, (File) getIntent().getSerializableExtra("file"));
        }
    }

    private void showTimeSelect() {
        if (timeBuilder == null) {
//            Calendar selectedDate = Calendar.getInstance();
//            Calendar startDate = Calendar.getInstance();
            //startDate.set(2013,1,1);
//            Calendar endDate = Calendar.getInstance();
            //endDate.set(2020,1,1);
            //正确设置方式 原因：注意事项有说明
//                startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DATE), startDate.get(Calendar.HOUR_OF_DAY), startDate.get(Calendar.MINUTE), startDate.get(Calendar.SECOND));
//            endDate.set(2030, 11, 31);
            timeBuilder = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {//选中事件回调
//                    if (timeStamp.getTime() <= System.currentTimeMillis()) {
//                        AppUtil.showToast(AddOrUpdateAlarmActivity.this, "提醒时间必须大于当前时间");
//                    } else {
//                        EventBusManager.post(new EventBusBean.Builder().id(12).intMsg(666).msg("啦啦，吃药时间到了").object(timeStamp.getTime()).build());
//                    }
                    timeStamp = date.getTime();
//                    tvTime.setText(DateUtil.formMillisToStr(timeStamp, DateUtil.hourToMinPattern));
                    birth = DateUtil.formMillisToStr(timeStamp, DateUtil.yearToDatePattern);
                    civAge.setRightText(birth);
                    AppUtil.showLog(DateUtil.formMillisToStr(timeStamp, DateUtil.yearToDatePattern));
                }
            }).setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                    .setCancelText("取消")//取消按钮文字
                    .setSubmitText("确定")//确认按钮文字
//                    .setTitleText("时间设置")//标题文字
//                    .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
//                    .isCyclic(true)//是否循环滚动
//                    .setTitleColor(getResources().getColor(R.color.black_333333))//标题文字颜色
                    .setContentTextSize(DisplayUtil.sp2px(RegisterMsgActivity.this, 13))
                    .setTextColorCenter(getResources().getColor(R.color.blue_2A8CFF))
                    .setTextColorOut(getResources().getColor(R.color.gray_AFAFAF))
                    .setSubmitColor(getResources().getColor(R.color.blue_2A8CFF))
                    .setCancelColor(getResources().getColor(R.color.gray_FF999999))
                    .setDividerColor(getResources().getColor(R.color.blue_2A8CFF))
                    .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
//                    .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
//                    .setRangDate(startDate, endDate)//起始终止年月日设定
                    .setLabel(null, null, null, "时", "分", null)//默认设置为年月日时分秒
                    .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .isDialog(false);//是否显示为对话框样式
        }
        timeBuilder.setType(new boolean[]{true, true, true, false, false, false});
        pvTime = timeBuilder.build();
//        pvTime.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss(Object o) {
//                EventBusManager.post(new EventBusBean.Builder().id(8).build());
//            }
//        });
        if (timeStamp != 0) {
            Calendar selectedDate = Calendar.getInstance();
            selectedDate.setTime(new Date(timeStamp));
            pvTime.setDate(selectedDate);
        } else {
            pvTime.setDate(Calendar.getInstance());
        }
        pvTime.show();
    }

    private void showHeightSelect() {
        // 注意：自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针
        // 具体可参考demo 里面的两个自定义布局
        for (int i = 20; i < 260; i++) {
            heightList.add(i + "");
        }
        if (heightPicker == null) {
            heightPicker = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    if (options1 >= 0 && options1 < heightList.size()) {
                        AppUtil.showLog("选中：" + heightList.get(options1));
                        height = heightList.get(options1);
                        civHeight.setRightText(height + "厘米");
                    } else {
                        AppUtil.showShortToast(RegisterMsgActivity.this, "数据有误");
                    }
                }
            })/*.setLayoutRes(R.layout.picker_select_user, new CustomListener() {
                @Override
                public void customLayout(View v) {
                    TextView tv_confirm = v.findViewById(R.id.tv_confirm);
                    tv_confirm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            heightPicker.dismiss();
                            heightPicker.returnData();
                        }
                    });
                }
            })*/.setContentTextSize(DisplayUtil.sp2px(RegisterMsgActivity.this, 13))
                    .setTextColorCenter(getResources().getColor(R.color.blue_2A8CFF))
                    .setTextColorOut(getResources().getColor(R.color.gray_AFAFAF))
                    .setSubmitColor(getResources().getColor(R.color.blue_2A8CFF))
                    .setCancelColor(getResources().getColor(R.color.gray_FF999999))
                    .setDividerColor(getResources().getColor(R.color.blue_2A8CFF))
                    .setCyclic(true, true, true)
                    .setLabels("厘米", null, null)/*.setDividerType(WheelView.DividerType.WRAP)*/.build();
        }
        heightPicker.setNPicker(heightList, null, null);
        heightPicker.setSelectOptions(177 - 20);
        heightPicker.show();
    }

    private void showWeightSelect() {
        // 注意：自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针
        // 具体可参考demo 里面的两个自定义布局
        for (int i = 0; i < 300; i++) {
            weightList.add((i + 1) + "");
        }
        if (weightPicker == null) {
            weightPicker = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    if (options1 >= 0 && options1 < weightList.size()) {
                        AppUtil.showLog("选中：" + weightList.get(options1));
                        weight = weightList.get(options1);
                        civWeight.setRightText(weight + "千克");
                    } else {
                        AppUtil.showShortToast(RegisterMsgActivity.this, "数据有误");
                    }
                }
            }).setContentTextSize(DisplayUtil.sp2px(RegisterMsgActivity.this, 13))
                    .setTextColorCenter(getResources().getColor(R.color.blue_2A8CFF))
                    .setTextColorOut(getResources().getColor(R.color.gray_AFAFAF))
                    .setSubmitColor(getResources().getColor(R.color.blue_2A8CFF))
                    .setCancelColor(getResources().getColor(R.color.gray_FF999999))
                    .setDividerColor(getResources().getColor(R.color.blue_2A8CFF))
                    .setCyclic(true, true, true)
                    .setLabels("千克", null, null)
                    .build();
        }
        weightPicker.setNPicker(weightList, null, null);
        weightPicker.setSelectOptions(59);
        weightPicker.show();
    }

    private void showPressureSelect() {
        // 注意：自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针
        // 具体可参考demo 里面的两个自定义布局
        for (int i = 30; i < 300; i++) {
            heightPressureList.add(i + "");
        }
        for (int i = 10; i < 200; i++) {
            lowPressureList.add(i + "");
        }
        if (pressurePicker == null) {
            pressurePicker = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    if (options1 >= 0 && options1 < heightPressureList.size()) {
                        AppUtil.showLog("选中：" + heightPressureList.get(options1));
                        pressure = heightPressureList.get(options1) + "/" + lowPressureList.get(option2);
                        civBloodPressure.setRightText(pressure + "mmHg");
                    } else {
                        AppUtil.showShortToast(RegisterMsgActivity.this, "数据有误");
                    }
                }
            }).setContentTextSize(DisplayUtil.sp2px(RegisterMsgActivity.this, 13))
                    .setTextColorCenter(getResources().getColor(R.color.blue_2A8CFF))
                    .setTextColorOut(getResources().getColor(R.color.gray_AFAFAF))
                    .setSubmitColor(getResources().getColor(R.color.blue_2A8CFF))
                    .setCancelColor(getResources().getColor(R.color.gray_FF999999))
                    .setDividerColor(getResources().getColor(R.color.blue_2A8CFF))
                    .isCenterLabel(true)
                    .setLabels(null, "mmHg", null)
                    .setCyclic(true, true, true)
                    .build();
        }
        pressurePicker.setNPicker(heightPressureList, lowPressureList, null);
        pressurePicker.setSelectOptions(120 - 30, 90 - 10);
        pressurePicker.show();
    }

    @Override
    public void onRegisterSuccess(BaseBean baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
                startActivity(new Intent(this, LoginByPwdActivity.class));
                ActivityStack.finishAllWithoutActivity(LoginByPwdActivity.class.getName());
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "注册失败，请重试");
        }
    }

    @Override
    public void onRegisterFailure() {
        hideProgress();
    }

    @Override
    protected void onDestroy() {
        if (loginAndRegisterModel != null) {
            loginAndRegisterModel.removeListener();
        }
        super.onDestroy();
    }
}
